import 'package:flutter/material.dart';

/**
 * GlobalKey 用法
 */
main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.teal, cardColor: Colors.orange),
      debugShowCheckedModeBanner: false,
      home: HYHomePage(),
    );
  }
}

class HYHomePage extends StatelessWidget {
  final GlobalKey<_HYHomeContentState> homeKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter Demo'),
      ),
      body: HYHomeContent(key: homeKey),
      backgroundColor: Colors.white,
      floatingActionButton: FloatingActionButton(
          child: Icon(Icons.add),
          backgroundColor: Colors.teal,
          onPressed: () {
            print(homeKey.currentState.message);
            print(homeKey.currentState.widget.name);
            homeKey.currentState.test();
          }),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    );
  }
}

class HYHomeContent extends StatefulWidget {
  final String name = 'madison';

  HYHomeContent({Key key}) : super(key: key);

  @override
  _HYHomeContentState createState() => _HYHomeContentState();
}

class _HYHomeContentState extends State<HYHomeContent> {
  String message = '123';

  void test() {
    print('_HYHomeContentState test function');
  }

  @override
  Widget build(BuildContext context) {
    return Text(message);
  }
}
